var express = require('express');
var router = express.Router();


var Post = require('../lib/mongo').Post;
var Comment = require('../lib/mongo').Comment;
var checkLogin = require('../middlewares/check').checkLogin;


router.get('/pic',function(req,res){
	var DH = req.query.DH;
	var XH = req.query.XH;
	var AttachXH = req.query.AttachXH;
	var value = {
			DH:DH,
			XH:XH,
			AttachXH:AttachXH
	}
	console.log('查询参数：')
	console.log(value)
	var result = {"error":"","data":[
		{"FilePath":"http://localhost:3000/assets/qq.png"},
		{"FilePath":"http://localhost:3000/assets/wechat.png"},
		{"FilePath":"http://localhost:3000/assets/alipay.png"}],
		"UserName":"songhuaiyin"}
	res.send(result);
})
router.post('/pic',function(req,res){
	console.log(req.fields);
	var result = {
			picPath:'http://localhost:3000/assets/qq.png'
	}
	res.send(result);
})
// GET /posts 所有用户或者特定用户的文章页
// eg: GET /posts?author=xxx
router.get('/', function (req, res, next) {
    // console.log("2222222222222222222"+req.query.search);
    // console.log(req.params);
    // console.log("4444444444444444444"+req.params.search);
    var query = {};
    if (req.query.searchText) {
        query.author = req.query.searchText;
        console.log(query);
    }
    Post.find(query)
        /* .populate({path: 'author', model: 'User'}) */
        .sort({_id: -1})
        .exec()
        .then(function (posts) {
            res.send(posts);
            // console.log(posts[0].created_at);//应该只能用在后端，如果在前端这么读肯定读不到created_at
        })
        .catch(next);
});

// POST /posts 发表一篇文章
router.post('/', checkLogin, function (req, res, next) {
	console.log("hello world");
	var title = req.fields.title;
    var content = req.fields.content;
    var url = req.fields.url;
    var author = req.fields.author;
    
    var post = {
    		title:title,
    		url:url,
    		content:content,
    		author:author
    };
    console.log(post);
  
    Post.create(post)
        .then(function (post) {
            // 此 post 是插入 mongodb 后的值，包含 _id
            res.send({success:'发表成功'});
        })
        .catch(next);
});

// GET /posts/:postId 单独一篇的文章页
router.get('/:postId', function (req, res, next) {
    var postId = req.params.postId;
    Post.findOne({_id: postId})
    .exec()// 获取文章信息
    .then(function (post) {
            // 此 post 是插入 mongodb 后的值，包含 _id
            res.send(post);
        })
        .catch(next);
    
});

// POST /posts/:postId/edit 更新一篇文章
router.post('/edit/:postId', checkLogin, function (req, res, next) {
    var postId = req.params.postId;
    var author = req.session.user._id;
    var title = req.fields.title;
    var content = req.fields.content;
    Post.update({author: author, _id: postId}, {$set: {title: title, content: content}})
        .exec()
        .then(function () {
            req.flash({'success':'编辑文章成功'});
        })
        .catch(next);
});

// GET /posts/:postId/remove 删除一篇文章
router.get('/remove/:postId', checkLogin, function (req, res, next) {
    var postId = req.params.postId;
    var author = req.session.user._id;

    Post.remove({author: author, _id: postId})
        .exec()
        .then(function (result) {
            // 文章删除后，再删除该文章下的所有留言
            if (result.result.ok && result.result.n > 0) {
                Comment.remove({postId: postId}).exec();
            }
            res.send({'success':'删除文章成功'});
        })
        .catch(next);
});

// POST /posts/:postId/comment 创建一条留言
router.post('/addComment', checkLogin, function (req, res, next) {
    Comment.create(req.fields)
        .then(function (result) {
            res.send(result);
        })
        .catch(next);
});

router.get('/getComments/:postId', checkLogin, function (req, res, next) {
    Comment
    .find({postId:req.params.postId})
    .sort({_id: -1})
        .then(function (results) {
            res.send(results);
        })
        .catch(next);
});

// GET /posts/:postId/comment/:commentId/remove 删除一条留言
router.get('/removeComment/:commentId', checkLogin, function (req, res, next) {
    var commentId = req.params.commentId;
    var author = req.session.user._id;

    Comment
        .remove({author: author, _id: commentId})
        .exec()
        .then(function () {
            res.send({'success':'删除留言成功'});
        })
        .catch(next);
});

module.exports = router;
